﻿using EPE.DALFactory;
using EPE.IDAL;
using EPE.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;

namespace EPE.BLL
{
    public class ShopsPricePara
    {
        private readonly IShopsPricePara dal = DataAccessFactory.CreateShopsPricePara();

        public DataSet GetAllByProject(string projectid)
        {
            return dal.GetAllByProject(projectid);
        }

        public DataTable GetAllListOnTable(string projectid, string companyid)
        {
            DataTable dt = dal.GetAllList(projectid, companyid).Tables[0];
            return dt;
        }

        public ObservableCollection<CShopsPriceParaTree> GetAllList(string projectid,string companyid)
        {
            ObservableCollection<CShopsPriceParaTree> ShopsPriceParaTree= new ObservableCollection<CShopsPriceParaTree>();

            DataTable dt = dal.GetAllList(projectid, companyid).Tables[0];

            string expression;
            expression = "parentid=0";
            DataRow[] dr = dt.Select(expression);
            if (dr.Length > 0)
            {
                for (int i = 0; i < dr.Length; i++)
                {
                    CShopsPriceParaTree p = new CShopsPriceParaTree(Convert.ToInt32(dr[i]["paraid"]), dr[i]["paraname"].ToString(), Convert.ToDecimal(dr[i]["paravalue"]), Convert.ToDecimal(dr[i]["resultvalue"]), dr[i]["property"].ToString(), dr[i]["comment"].ToString(), dr[i]["projectname"].ToString());
                    p.MarginLeft = 10;

                    AddChildNode(dr[i]["paraid"].ToString(), ref p, dt, p.MarginLeft);

                    ShopsPriceParaTree.Add(p);
                }

            }
           

            return ShopsPriceParaTree;
        }

        /// <summary>
        /// 递归添加子节点
        /// </summary>
        /// <param name="parentid">子节点参数</param>
        /// <param name="p">父节点</param>
        /// <param name="dt">数据表</param>
        /// <param name="marginleft">左边空格数</param>
        private void AddChildNode(string parentid, ref CShopsPriceParaTree p,DataTable dt,double marginleft)
        {
            string expression;
            expression = "parentid=" + parentid;
            DataRow[] dr = dt.Select(expression);
            if (dr.Length > 0)
            {
                for (int i = 0; i < dr.Length; i++)
                {
                    CShopsPriceParaTree pn = new CShopsPriceParaTree(Convert.ToInt32(dr[i]["paraid"]), dr[i]["paraname"].ToString(), Convert.ToDecimal(dr[i]["paravalue"]), Convert.ToDecimal(dr[i]["resultvalue"]), dr[i]["property"].ToString(), dr[i]["comment"].ToString(), dr[i]["projectname"].ToString());
                    pn.MarginLeft = marginleft + 30;
                    pn.IsVisible = Visibility.Collapsed;
                    AddChildNode(dr[i]["paraid"].ToString(), ref pn, dt, p.MarginLeft);

                    p.Subordinates.Add(pn);
                }

               
            }
            else
            {
                return;
            }
        }


    }
}
